package com.autoabacus.dal.dao;


import java.io.Serializable;   
import java.lang.reflect.ParameterizedType;   
import java.sql.SQLException;   
import java.util.Collection;    
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;   
import org.apache.commons.logging.LogFactory;   
import org.hibernate.Criteria;   
import org.hibernate.HibernateException;    
import org.hibernate.Query;   
import org.hibernate.Session;   
import org.hibernate.criterion.DetachedCriteria;   
import org.hibernate.criterion.Projections;   
import org.springframework.dao.DataAccessException;   
import org.springframework.orm.hibernate3.HibernateCallback;   
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
import org.springframework.stereotype.Repository;

@SuppressWarnings("unchecked")
public  class HibernateGenericDao<T, ID extends Serializable> implements IGenericDao<T,ID> {

    private Log logger = LogFactory.getLog(getClass());
    
    @Resource
    private HibernateTemplate hibernateTemplate;
    
    protected Class<T> entityClass;   
    
    public HibernateGenericDao() {   
    	
    }   
  
    protected Class getEntityClass() {   
        if (entityClass == null) {   
            entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];   
            logger.debug("T class = " + entityClass.getName());   
        }   
        return entityClass;   
    }   
  
    public void saveOrUpdate(T t) throws DataAccessException {   
        this.hibernateTemplate.saveOrUpdate(t);   
    }   
  
    public T load(ID id) throws DataAccessException {   
        T load = (T) hibernateTemplate.load(getEntityClass(), id);   
        return load;   
    }   
  
    public T get(ID id) throws DataAccessException {   
        T load = (T) hibernateTemplate.get(getEntityClass(), id);   
        return load;   
    }   
  
    public boolean contains(T t) throws DataAccessException {   
        return hibernateTemplate.contains(t);   
    }      
  
    public void delete(T t) throws DataAccessException {   
        hibernateTemplate.delete(t);   
    }   
  
    public void deleteAll(Collection<T> entities) throws DataAccessException {   
        hibernateTemplate.deleteAll(entities);   
    }   
  
    public List<T> find(String queryString, Object value) throws DataAccessException {   
        List<T> find = (List<T>) hibernateTemplate.find(queryString, value);   
        return find;   
    }   
  
    public List<T> find(String queryString, Object[] values) throws DataAccessException {   
        List<T> find = (List<T>) hibernateTemplate.find(queryString, values);   
        return find;   
    }   
  
    public List<T> find(String queryString) throws DataAccessException {   
        return (List<T>) hibernateTemplate.find(queryString);   
    }   
  
  
  
    public void refresh(T t) throws DataAccessException {   
        hibernateTemplate.refresh(t);   
    }   
  
    public Serializable save(T t) throws DataAccessException {   
        return hibernateTemplate.save(t);   
    }   
  
    public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException {   
        hibernateTemplate.saveOrUpdateAll(entities);   
    }   
  
 
  
    public void update(T t) throws DataAccessException {   
        hibernateTemplate.update(t);   
    }   
  
    public List<T> list() throws DataAccessException {   
        return hibernateTemplate.loadAll(getEntityClass());   
  
    }   
  
    public List<T> findByNamedQuery(String queryName) throws DataAccessException {   
        return hibernateTemplate.findByNamedQuery(queryName);   
    }   
  
    public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException {   
        return hibernateTemplate.findByNamedQuery(queryName, value);   
    }   
  
    public List<T> findByNamedQuery(String queryName, Object[] values) throws DataAccessException {   
        return hibernateTemplate.findByNamedQuery(queryName, values);   
    } 
}
